package com.bdqn.web.converter;

import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 自定义的字符串转日期的转换器
 */
public class String2DateConverter implements Converter<String, Date> {
    private Logger logger = Logger.getLogger(String2DateConverter.class);
    private String datePattern;

    // 修改为构造函数
    public String2DateConverter(String datePattern) {
        logger.info("加载 String2DateConverter");
        this.datePattern = datePattern;
    }

    @Override
    public Date convert(String s) {
        Date date = null;
        try {
            date = new SimpleDateFormat(datePattern).parse(s);
            logger.info("字符串转换成日期成功 " + date);
        } catch (ParseException e) {
            logger.error("字符串转换成日期失败 " + s);
            e.printStackTrace();
        }
        return date;
    }
}